﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using System.Data;
using MxTol.API.Helpers;
using MxTol.Managers;

namespace MxTol.API.Controllers
{
    /// <summary>
    /// 会计基础题库API
    /// </summary>
    public class BaController : ApiController
    {
        // 返回json数据格式
        private static readonly JsonMediaTypeFormatter JSON_FORMATTER = new JsonMediaTypeFormatter();

        /// <summary>
        /// 获取会计基础20道单选题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetSingle(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewSingle = isNew == 0 ? false : true;
                    table = BaSingleManager.GetSingle(isNewSingle);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取会计基础单选题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"single",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取会计基础20道多选题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetMultiple(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewMultiple = isNew == 0 ? false : true;
                    table = BaMultipleManager.GetMultiple(isNewMultiple);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取会计基础多选题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"multiple",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取会计基础20道判断题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetJudge(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewJudge = isNew == 0 ? false : true;
                    table = BaJudgeManager.GetJudge(isNewJudge);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取会计基础判断题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"judge",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取会计基础所有题型
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetBa(string key, int isNew = 0)
        {
            DataTable singleTable = null;
            DataTable multipleTable = null;
            DataTable judgeTable = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewFr = isNew == 0 ? false : true;

                    singleTable = BaSingleManager.GetSingle(isNewFr);
                    multipleTable = BaMultipleManager.GetMultiple(isNewFr);
                    judgeTable = BaJudgeManager.GetJudge(isNewFr);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取会计基础所有题型接口失败", ex);
            }

            Dictionary<string, object> json = new Dictionary<string, object>()
            {
                {"single",singleTable},
                {"multiple",multipleTable},
                {"judge",judgeTable},
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }
    }
}
